﻿using System;

namespace NetCore.Extensions
{
    public static class DateTimeExtensions
    {
        /// <summary>
        /// 格式化时间为字符串格式
        /// </summary>
        public static string TextForDate(this DateTime date, string format = NetCoreConsts.DateFormatString)
        {
            return date.ToString(format);
        }
        /// <summary>
        /// 获取该日期中的时间部分 HH:mm:ss.ttt
        /// </summary>
        public static TimeSpan ToTime(this DateTime date)
        {
            return new TimeSpan(0, date.Hour, date.Minute, date.Second, date.Millisecond);
        }
        /// <summary>
        /// 设置开始日期
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime SetStartDate(this DateTime date)
        {
            return date.Date.AddSeconds(-1);
        }
        /// <summary>
        /// 设置结束日期
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime? SetEndDate(this DateTime? date)
        {
            return date?.Date.AddDays(1).AddSeconds(-1);
        }
    }
}
